package com.tianya.forum.services.impl;


import com.tianya.forum.common.AppResult;
import com.tianya.forum.common.ResultCode;
import com.tianya.forum.dao.MessageMapper;
import com.tianya.forum.exception.ApplicationException;
import com.tianya.forum.model.Message;
import com.tianya.forum.model.User;
import com.tianya.forum.services.IMessageService;
import com.tianya.forum.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private IUserService userService;


    @Override
    public void create(Message message) {
        if(message == null || message.getContent() == null
                || message.getReceiveUserId() == null || message.getPostUserId() == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 校验接收者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1) {
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 设置默认值
        message.setState((byte)0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用DAO
        int row = messageMapper.insert(message);
        if (row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new
                    ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public Integer selectUnreadCount(Long userId) {
        if(userId == null ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Integer rusult = messageMapper.selectUnreadCount(userId);
        return rusult;
    }


    @Override
    public Message selectById(Long id) {
        // ⾮空校验
        if (id == null) {
            // 记录⽇志
            log.info(ResultCode.ERROR_IS_NULL.toString());
            // 抛出异常
            throw new
                    ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 查询结果
        Message result = messageMapper.selectByPrimaryKey(id);
        // 返回结果
        return result;

    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new
                    ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }


    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null){
            // 打印⽇志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existMessage == null || existMessage.getDeleteState() == 1){
            // 打印⽇志
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        // 更新状态为已回复
        updateStateById(repliedId, (byte) 2);
        // 回复的内容写⼊数据库
        create(message);
    }
}
